package com.qd.common.sys.domain.sendmsg;

import com.alibaba.fastjson2.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class MsgContentProperties {
    /**
     * 属性
     */
    private HashMap<String, MsgContentProperty> properties;

    public void free() {
        Optional.ofNullable(properties).ifPresent(HashMap::clear);
    }

    public void addProp(String name, String value) {
        if (properties == null) {
            properties = new HashMap<>();
        }
        properties.put(name, new MsgContentProperty(value));
    }

    public void addProp(String name, String value, String color) {
        if (properties == null) {
            properties = new HashMap<>();
        }
        properties.put(name, new MsgContentProperty(value, color));
    }

    public void removeProp(String name) {
        if (properties != null) {
            properties.remove(name);
        }
    }

    public boolean contains(String name) {
        if (properties != null) {
            return properties.containsKey(name);
        }
        return false;
    }

    public MsgContentProperty getProp(String name) {
        if (properties != null) {
            return properties.get(name);
        }
        return null;
    }

    public boolean isEmpty() {
        return null == properties || properties.isEmpty();
    }

    public int size() {
        if (properties != null) {
            return properties.size();
        }
        return 0;
    }

    public Map<String, MsgContentProperty> getProperties() {
        if (properties == null) {
            return java.util.Collections.emptyMap();
        }
        return properties;
    }

    public JSONObject toJson() {
        JSONObject json = new JSONObject();
        if (properties != null) {
            for (Map.Entry<String, MsgContentProperty> entry : properties.entrySet()) {
                JSONObject v = new JSONObject();
                v.put("value", entry.getValue().getValue());
                v.put("color", entry.getValue().getColor());
                json.put(entry.getKey(), v);
            }
        }
        return json;
    }

    public void read(JSONObject content) {
        JSONObject jsonObject = content.getJSONObject("properties");
        if (jsonObject != null) {
            properties = new HashMap<>();
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                if (entry.getValue() instanceof JSONObject) {
                    MsgContentProperty prop = new MsgContentProperty();
                    prop.read((JSONObject) entry.getValue());
                    properties.put(entry.getKey(), prop);
                }
            }
        }
    }

    public void write(JSONObject content) {
        if (properties != null) {
            JSONObject jsonObject = new JSONObject();
            content.put("properties", jsonObject);
            for (Map.Entry<String, MsgContentProperty> entry : properties.entrySet()) {
                JSONObject prop = new JSONObject();
                entry.getValue().write(prop);
                jsonObject.put(entry.getKey(), prop);
            }
        }
    }
}
